package com.itheima.myiotest1;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test1 {
    public static void main(String[] args) throws IOException {

        //上网爬取姓氏和名字组成假性数据

        //表示中文范围[\u4E00-\u9FA5]

        //http://www.baijiaxing.net.cn/  姓
        //https://www.meimeiming.com/zhouyi/3335.html  男生名字
        //https://www.meimeiming.com/zhouyi/8347.html  女生名字

        //字符串表示网址
        String s1 = "https://www.diyifanwen.com/tool/baijiaxing/";
        String s2 = "https://www.meimeiming.com/zhouyi/3335.html";
        String s3 = "https://www.meimeiming.com/zhouyi/8347.html";
        String s4 = "https://www.haoming8.cn/baobao/7641.html";
        String s5 = "http://www.haoming8.cn/quming/105300.html";
        String familyName = "http://www.baijiaxing.net.cn/";
        String s7 = "https://www.meimeiming.com/zhouyi/5143.html";
        String boyName = "http://www.cimingwang.com/nanhaimingzi/948.html";
        String girlName = "https://qiming.hao86.com/view/141985/";

        //正则表达式
        String familyNameRegex = "([\\u4E00-\\u9FA5]{4})(，|。)";
        String boyNameRegex = "([\\u4E00-\\u9FA5]{2})(、)";
        String girlNameRegex = "(、)([\\u4E00-\\u9FA5]{2})";


        //爬取网页内容
        String familyNameStr = paQu(familyName);
        //System.out.println(s);
        String boyNameStr = paQu(boyName);
        //System.out.println(str2);
        String girlNameStr = paQu(girlName);
        //System.out.println(str3);



        //筛选爬取到的网页内容
        //姓氏
        ArrayList<String> tempFamilyNamelist = shaiXuan(familyNameStr, familyNameRegex,1);
        //System.out.println(templist1);
        //将姓氏拆开
        ArrayList<String> familyNameList = new ArrayList<>();
        for (String s : tempFamilyNamelist) {
            char[] charArray = s.toCharArray();
            for (char c : charArray) {
                familyNameList.add(c+"");
            }
        }
        //System.out.println(list1);

        //男生名字
        ArrayList<String> boyNameList = shaiXuan(boyNameStr, boyNameRegex,1);
        //System.out.println(list2);

        //女生名字
        ArrayList<String> girlNameList = shaiXuan(girlNameStr, girlNameRegex,2);
        //System.out.println(list3);

        //将筛选到的名字和姓氏组合

        //得到最终信息
        ArrayList<String> result = zuHe(familyNameList, boyNameList, girlNameList,5,5);
        Collections.shuffle(result);
        //System.out.println(result);

        //写到本地文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("myiotest\\test.txt"));
        for (String s : result) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();

    }

    private static ArrayList<String> zuHe(ArrayList<String> familyNameList, ArrayList<String> boyNameList, ArrayList<String> girlNameList, int boyCount, int girlCount) {

        //返回 姓名-性别-年龄
        //最终的集合
        ArrayList<String> result = new ArrayList<>();

        //随机生成年龄
        Random r=new Random();

        //打乱顺序
        Collections.shuffle(familyNameList);
        Collections.shuffle(boyNameList);
        Collections.shuffle(girlNameList);

        //随机生成姓名
        for (int i = 0; i < boyCount; i++) {
            String s = familyNameList.get(i) + boyNameList.get(i)+"-男-"+(r.nextInt(7) + 18);
            //去重
            if(result.contains(s)){
                continue;
            }
            result.add(s);
        }

        for (int i = 0; i < girlCount; i++) {
            String s = familyNameList.get(i) + boyNameList.get(i)+"-女-"+(r.nextInt(7) + 18);
            if(result.contains(s)){
                continue;
            }
            result.add(s);
        }

        return result;
    }


    private static ArrayList<String> shaiXuan(String s, String regex,int index) {

        //将爬取到的网页内容通过正则表达式筛选

        //获取正则表达式对象
        Pattern p = Pattern.compile(regex);
        //获取匹配器对象
        Matcher m = p.matcher(s);
        //开始找
        //创建集合
        ArrayList<String> list = new ArrayList<>();
        while (m.find()){
            //去重
            if(list.contains(m.group(index))){
                continue;
            }
            //获取匹配到的内容添加到集合
            list.add(m.group(index));
            //System.out.println(m.group());
        }
        //返回
        return list;
    }

    private static String paQu(String s) throws IOException {
        //创建URL对象
        URL url1 = new URL(s);
        //打开网络连接
        URLConnection opened = url1.openConnection();
        //字节流爬取网址数据,但要爬取的是中文，通过转换流转为字符流爬取
        InputStreamReader isr = new InputStreamReader(opened.getInputStream());
        int i;
        //创建sb来存放爬取到的数据
        StringBuilder sb = new StringBuilder();
        while ((i = isr.read()) != -1) {
            sb.append((char) i);
        }
        //将容器返回
        return sb.toString();
    }
}
